#####
#####
###     Perform unfolding analysis on data
###     from 1982 through 2011
#####
#####

###
###   Load "cgmunf" package
###

library(cgmunf)

###
###  Read state policy spending data from file, "spending
###  data, 1982-2011, with names.txt"
###

states <- read.table(file.choose(), header = TRUE)

states[1:10,]

###
###   Convert spending values to proportions
###

###
###  First create matrix of summed spending 
###  for each state/year
###

spending.sum <- apply(states[,3:11], 1, sum)

sum.matrix <- matrix(spending.sum, nrow = 1500, ncol = 9)

sum.matrix[1:10,]

###
###   Second, create data frame "states2" which
###   contains spending as proportions rather
###   than dollar values
###

states2 <- states

states2[,3:11] <- states2[,3:11] / sum.matrix

states2[1:10,]

###
###   Third, data frame "states3" reflects the
###   proportions by subtracting them all from 1.0
###

states3 <- states2

states3[, 3:11] <- matrix(1, nrow = 1500, ncol = 9) - states2[, 3:11]

states3[1:5,]

###
###   Create new row labels containing state 
###   name and year
###

new.labels <- paste(states3$state, states3$year)

rownames(states3) <- new.labels

states3 <- states3[,3:11]

states3[1:15,]

###
###   Carry out the metric unfolding analysis using
###   the "unfold" function from package "cgmunf"
###

try8211 <- unfold(states3, start.stims <- c(1, rep(-1, 3), rep(1,5)))

try8211$spl

try8211$history

###
###   Merge unfolded state ideal points (i.e., priority
###   scores) with the data frame "states"
###

states$idl.pt <- try8211$ipl

###
###   Create trellis display of state priority scores
###   over time, by state
###

library(lattice)

xyplot(idl.pt ~ year | state, data = states,
       aspect = .75, layout = c(5, 5, 2),
       col = "black", cex = .5,
       scales = list(cex = .65, x = list(at = c(1982, 1992, 2002, 2012),
                              labels = c("1982", "1992", "2002", "2012")))
)      


getwd()

states$ideal.pt <- round(states$idl.pt, digits = 6)

states[1:15, c(1, 2, 13)]

###
###   Write "raw" state ideal points
###   to an external file
###

write.table(states[, c(1, 2, 13)],
            file = "state ideal points, 1982-2011.txt",
            quote = FALSE,
            row.names = FALSE,
            col.names = FALSE)


#####
#####
###     Peform linear transformation on ideal points
###     so that mean ideal point is zero, and differences
###     in ideal points correspond to differences in 
###     state spending on collective goods
#####
#####

###
###   First, obtain proportion of total spending that
###   goes to collective goods policy areas
###

states$coll.goods <- states2$educ + states2$highways + 
  states2$law.enforcement + states2$parks.natres + states2$govt.admin

###
###   Regress collective goods on ideal points
###

normalize <- lm(coll.goods ~ idl.pt, data = states)

summary(normalize)

summary(predict(normalize))

###
###   Obtain mean of predicted values from regression
###

meanval <- mean(predict(normalize))

states$adjusted.ideal.points <- predict(normalize) - meanval

cor(states$idl.pt, states$adjusted.ideal.points)

###
###   The variable, "adjusted.ideal.points", contains yearly state
###   ideal points, with values adjusted so that differences 
###   in the ideal points correspond to differences in the proportions
###   that states spend on collective goods. This variable is
###   also centered to a mean value of zero.
###

###
###   Create new data frame, containing state, year, 
###   and adjusted ideal point values
###

states.adjusted <- states[, c("state", "year", "adjusted.ideal.points")]

states.adjusted$adjusted.ideal.points <-
round(states.adjusted$adjusted.ideal.points, digits = 8)

write.table(states.adjusted,
            file = "adjusted state ideal points, 1982-2011.txt",
            quote = FALSE,
            row.names = FALSE,
            col.names = FALSE)
